layui.use(['layer','form','table','element'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element;


    //初始化数据表格
     table.render({
         elem:"#data"
         ,height: 440
         ,url:"/admin/getNews"
         ,cols: [[
              {field:'id',width:60,title: 'ID', sort: true}
             ,{field:'title',title: '标题',width: 180   }
             ,{field:'showCreateTime',width:180,title: '发送时间',sort: true}
             ,{field:'isRead',width:180,title: '是否已读',templet:'#readTem'}
             ,{field:'',width:170,title: '操作',toolbar: '#barDemo'}
         ]]
         ,page:true
         ,id:"table"
     });

     //监听工具条
     table.on('tool(dataTable)',function (obj) {
         var data = obj.data;
         if(obj.event == 'detail'){

             //将消息置为已读
             $.ajax({
                 type:"POST"
                 ,url:"/driver/newsRead"
                 ,data:{
                     id:data.id
                 }
                 ,success:function (res) {
                     //查看消息详情
                     if(res.flag){
                         layer.open({
                             type: 1
                             ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                             ,id: 'layerDemo'+'auto' //防止重复弹出
                             ,content: '<div style="padding: 20px 100px;">'+ data.content +'</div>'
                             ,btn: '关闭'
                             ,btnAlign: 'c' //按钮居中
                             ,shade: 0 //不显示遮罩
                             ,yes: function(){
                                 layer.closeAll();
                                 table.reload('table', {
                                     page: {
                                         curr: 1 //重新从第 1 页开始
                                     }
                                 });
                             }
                         });

                     }else{
                         layer.msg("消息状态置为已读失败！");
                     }
                 }
                 ,error:function () {
                     layer.msg("系统异常");
                 }

             });


         }else {
             //编辑项目
            layer.msg("操作错误！");
         }
     });

    function rightMenu(){

        $(".layui-tab-title li",window.parent.document).on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu",window.parent.document);
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li",window.parent.document).click(function(){
            $(".rightmenu").hide();
        });

    }

    function iframeWH(){
        var H = $(window).height();
        $("iframe",window.parent.document).css("height",H+"px");
    }
     
     //搜索
    $("#search").on("click",function () {
        table.reload("data",{
            page:{
                curr:1
            }
            ,where:{
                used:$("#used").val(),
            }
        })
    });;

    $("#add").on("click",function () {

        parent.layui.element.tabAdd('tabs',{
            title:"广告新增",
            content:"<iframe  src='/admin/advert/toAdd'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
            id:"advert_add"
        });
        rightMenu();
        iframeWH();
        parent.layui.element.tabChange('tabs','advert_add');


    })


});
